package com.jeeplus.modules.work.queue.utils;
import com.jeeplus.modules.work.queue.service.QueueService;
import okhttp3.*;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*
异步/公共发送请求类
*/
@Component
public class QueueSend {
    /**
     * 窗口人员变更通知接口
     */
    @Value("${AlterationInformURL}")
    private String AlterationInformURL;
    /**
     * 排队叫号接口
     */
    @Value("${QueuingURL}")
    private String QueuingURL;

    @Autowired
    private  QueueService service;
    private static final Logger logger = LoggerFactory.getLogger(QueueSend.class);

    @Async()
    public String windowStatus(String sysno,String winNo,String online){
        logger.error("监察对接-窗口状态变动");
        int index = winNo.indexOf("号窗口");
        if(index!=-1){
            winNo=winNo.substring(0,index);
        }
        String windowId = service.getWindowId(sysno, winNo);
        String url="";
        if (!"".equals(windowId) && null!=windowId){
             url= AlterationInformURL+"?window="+windowId
                     +"&online="+online;
            String s = webService(url);
            logger.error("监察对接url:"+url+"\n监察对接-窗口状态变动:"+s);
        }
        return url;
    }
    @Async()
    public void updateWindowStatus(){
        logger.error("监察对接-每天早上9点窗口更新在线");
        List <Map<String, String>> windosInfo = service.getWindosInfo();
        for (int i = 0; i < windosInfo.size(); i++) {
            Map<String, String> map = windosInfo.get(i);
            String url= AlterationInformURL+"?window="+map.get("id")
                    +"&online=1";
            String s = webService(url);
            logger.error("监察对接-窗口状态变动:"+s);
        }



    }
    /**
     * 对接—排队叫号信息_开始叫号
     * @param number 叫号编号
     * @return 排队叫号信息
     */
    @Async()
    public String queuingInfoStart(String number,String sysno,String winNo,String userNo){
        logger.error("监察对接—办理开始");
        int index = winNo.indexOf("号窗口");
        if(index!=-1){
            winNo=winNo.substring(0,index);
        }
        String windowId = service.getWindowId(sysno, winNo);
        String staffId = service.getStaffId(sysno, userNo);
        Map<String, Object> map = service.queuingInfoStart(number,sysno);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(new Date());
        String url = QueuingURL+"?type=1" +
                "&id="+map.get("id") +
                "&business="+ map.get("business")+
                "&flownum="+map.get("flownum") +
                "&name=" +map.get("name")+
                "&mobile="+ map.get("mobile")+
                "&idcard="+ map.get("idcard")+
                    "&window="+ windowId+
                "&workman="+ staffId+
                "&style=2" +
                "&taketime="+ map.get("taketime")+
                "&calltime="+format;
        String s = webService(url);
        logger.error("监察对接办理开始："+s+"--"+url);
        return s;
    }
    /**
     * 对接—排队叫号信息_结束
     * @param number 叫号编号
     * @return 排队叫号信息
     */

    @Async()
    public String queuingInfoEnd(String number,String sysno){
        logger.error("监察对接—办理结束");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(new Date());
        Map<String, Object> map = service.queuingInfoStart(number,sysno);
        //调用接口
        String url = QueuingURL + "?type=2" +
                "&id=" +map.get("id")+
                "&endtime="+format+
                "&style=4";
        String s = webService(url);
        logger.error("监察对接办理结束："+s);
        return s;
    }


    /**
     * okhttp调用接口方法
     * @param url 路径
     * @return 其他接口返回的数据
     */
    public String webService(String url){
        String string ="";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            Request request = new Request.Builder()
                    .url(url)
                    .method("GET", null)
                    .build();
            Response response = client.newCall(request).execute();
            string = response.body().string().toString();
        } catch (ClientProtocolException e) {
            System.err.println("Http协议出现问题");
            e.printStackTrace();
        } catch (ParseException e) {
            System.err.println("解析错误");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO异常");
            e.printStackTrace();
        }
        return string;
    }







}
